ActionController::Routing::Routes.draw do |map|
  # The priority is based upon order of creation: first created -> highest priority.
  
  # Sample of regular route:
  # map.connect 'products/:id', :controller => 'catalog', :action => 'view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
  # This route can be invoked with purchase_url(:id => product.id)

  # You can have the root of your site routed by hooking up '' 
  # -- just remember to delete public/index.html.
  
  # Root index
  map.connect '', :controller => "welcome"
  map.connect '/u', :controller => 'welcome'
  
  # Naming Routes
  
  map.welcome 'welcome',
              :controller => 'welcome'
  
  map.admin 'admin', 
            :controller =>'admin/dashboard', 
            :action => 'index'
  
  map.login 'login', 
            :controller => 'accounts', 
            :action => 'login'
            
  map.logout 'logout', 
               :controller => 'accounts', 
               :action => 'logout'
  
  map.index '/u/:screen_name',
            :controller => 'entries',
            :action => 'index'
            
  map.signup 'signup',
             :controller => 'accounts',
             :action => 'signup'
  
  map.rss 'u/:screen_name/rss',
          :controller => 'xml',
          :action => 'rss'
          
  map.entry_show 'u/:screen_name/entries/:id',
                 :controller => 'entries',
                 :action => 'show',
                 :id => /\d+/
  
  date_options = { :year => /\d{4}/, :month => /(?:0?[1-9]|1[12])/, :day => /(?:0[1-9]|[12]\d|3[01])/ }

  map.with_options(date_options.merge(:controller => 'entries')) do |date|
    date.with_options(:action => 'index') do |finder|
      finder.connect 'u/:screen_name/:year',
    	  :month => nil, :day => nil
      finder.connect 'u/:screen_name/:year/page/:page', 
        :month => nil, :day => nil, :page => /\d+/
      finder.connect 'u/:screen_name/:year/:month',
        :day => nil, :page => 1
      finder.connect 'u/:screen_name/:year/:month/page/:page', 
        :day => nil, :page => /\d+/
      finder.connect 'u/:screen_name/:year/:month/:day'
      finder.connect 'u/:screen_name/:year/:month/:day/page/:page', :page => /\d+/
    end
  end
  
  map.connect 'u/:screen_name/entries/page/:page',
    :controller => 'entries', :action => 'index', :page => /\d+/
          
  %w{entries}.each do |i|
    map.connect "u/:screen_name/#{i}", :controller => "#{i}", :action => 'index'
    map.connect "u/:screen_name/#{i}/:action", :controller => "#{i}"
    map.connect "u/:screen_name/#{i}/:action/:id", :controller => "#[i}", :id => /\d+/
  end

  # Install the default route as the lowest priority.
  map.connect 'u/:screen_name/:controller/:action/:id.:format'
  map.connect 'u/:screen_name/:controller/:action/:id'
  
  map.connect 'password/recover/:token', :controller => 'password', :action => 'recover_password'
  
  map.connect ':controller/:action/:id.:format'
  map.connect ':controller/:action/:id'
end
